//
//  YJBasicinformationController.m
//  SnackCourier
//
//  Created by qianfeng01 on 15/7/21.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "YJBasicinformationController.h"

#import "YJImmediatelygrabViewCell.h"
#import "YJconnectionCell.h"

#import "YJImmediatelygrabControllerModel.h"
#import "YJImmediatelygrabModel.h"

#import "AFNetworking.h"

#import "CreateSales.h"

@interface YJBasicinformationController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray *immediateArray;
@property (nonatomic,strong)NSMutableArray *connectArray;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)AFHTTPRequestOperationManager *manager;


@end

@implementation YJBasicinformationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray =[[NSMutableArray alloc] init];
    self.immediateArray =[[NSMutableArray alloc] init];
    self.connectArray =[[NSMutableArray alloc] init];
    
    [self initUI];
    [self loadData];
    
}

-(void)initUI
{
    self.tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64) style:(UITableViewStyleGrouped)];
    self.tableView.delegate =self;
    self.tableView.dataSource =self;
    self.tableView.rowHeight =320;
    [self.tableView registerNib:[UINib nibWithNibName:@"YJImmediatelygrabViewCell" bundle:nil] forCellReuseIdentifier:@"YJImmediatelygrabViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"YJconnectionCell" bundle:nil] forCellReuseIdentifier:@"YJconnectionCell"];
    [self.view addSubview:self.tableView];
    
}
-(void)loadData{
    
    NSString *path =[YJHelper getFullPathWithFile:self.urlStr];
    
    BOOL isExist =[[NSFileManager defaultManager] fileExistsAtPath:path];
    BOOL isTimeout =[YJHelper isTimeOutWithFile:self.urlStr timeOut:24*60*60];
    if ((isExist==YES)&&(isTimeout==NO) ) {
        [self.dataArray removeAllObjects];
        NSData *data =[NSData dataWithContentsOfFile:[YJHelper getFullPathWithFile:self.urlStr]];
        NSDictionary *dicts =[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *datadic =dicts[@"data"];
        YJImmediatelygrabControllerModel *model =[[YJImmediatelygrabControllerModel alloc] init];
        [model setValuesForKeysWithDictionary:datadic];
        [self.immediateArray addObject:model];
        
        [self.dataArray addObject:self.immediateArray];
        NSArray *array =datadic[@"guessLikeList"];
        for (NSDictionary *dict in array) {
            YJImmediatelygrabModel *model =[[YJImmediatelygrabModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.connectArray addObject:model];
            
        }
        
        [self.dataArray addObject:self.connectArray];
        [self.tableView reloadData];
    }
    
    self.manager =[AFHTTPRequestOperationManager manager];
    self.manager.responseSerializer =[AFHTTPResponseSerializer serializer];
    __weak typeof (self) weakSelf=self;
    [self.manager GET:self.urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary *dicts =[NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *data =dicts[@"data"];
            YJImmediatelygrabControllerModel *model =[[YJImmediatelygrabControllerModel alloc] init];
            [model setValuesForKeysWithDictionary:data];
            [weakSelf.immediateArray addObject:model];
            
            [weakSelf.dataArray addObject:weakSelf.immediateArray];
            NSArray *array =data[@"guessLikeList"];
            for (NSDictionary *dict in array) {
                YJImmediatelygrabModel *model =[[YJImmediatelygrabModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [weakSelf.connectArray addObject:model];
 
            }
            
            [weakSelf.dataArray addObject:weakSelf.connectArray];
            [weakSelf.tableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        YJImmediatelygrabViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"YJImmediatelygrabViewCell" forIndexPath:indexPath];
        YJImmediatelygrabControllerModel *model =self.immediateArray[indexPath.row];
        [cell showDataWithModel:model controller:self];
        return cell;
    } else {
        YJconnectionCell *cell=[tableView dequeueReusableCellWithIdentifier:@"YJconnectionCell" forIndexPath:indexPath];
        [cell showDataWithArray:self.connectArray myBlock:^(NSInteger id) {
            YJImmediatelygrabController *controller =[CreateSales createVC:id];
            controller.title =@"商品详情";
            [self.navigationController pushViewController:controller animated:YES];
        }];
        return cell;
    }

}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==1) {
        return 30;
    }
    return 1;
}


-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    
    if (section ==0) {
        return 1;
    }
    return 30;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==1) {
        UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
        UIView *redView =[[UIView alloc] initWithFrame:CGRectMake(10, 8, 4, 14)];
        redView.backgroundColor =[UIColor redColor];
        redView.layer.masksToBounds =YES;
        redView.layer.cornerRadius =2;
        [view addSubview:redView];
        
        UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(redView.frame.size.width+13, 8, self.view.frame.size.width, 14)];
        label.text =@"猜你喜欢";
        [view addSubview:label];
        return view;
    }
    return nil;
}


@end
